package com.maxjay.system.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.maxjay.system.dao.ResourceDao;

@Repository
@SuppressWarnings({"rawtypes", "unchecked"})
public class ResourceDaoJdbc extends BaseDaoJdbc implements ResourceDao {
	
	private static Logger logger = Logger.getLogger(ResourceDaoJdbc.class);

	@Override
	public Set<String> getResourceRoles(String resourceId) {
		String sql = "select t1.ROLE_ID from T_SYS_ROLE_RESOURCE t1, T_SYS_RESOURCE t2 " +
				" where t1.RESOURCE_ID = t2.OBJ_ID and t2.OBJ_ID=?";
		logger.debug("查询资源[RESOURCE_ID:" + resourceId + "]对应的角色SQL：" + sql);
		return (Set<String>)this.getJdbcTemplate().query(sql, new Object[]{ resourceId }, new ResultSetExtractor() {
			
			@Override
			public Object extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				Set<String> roleIds = new HashSet<String>();
				while(rs.next()){
					roleIds.add(rs.getString(1));
				}
				return roleIds;
			}
		});
	}

}
